
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nft_shop/market/page/details/works_details_page.dart';
import 'package:nft_shop/my/model/models.dart';
import 'package:nft_shop/my/page/notice/home_announcement_page.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/status_view.dart';

class SiftNoticePage extends StatefulWidget {
  const SiftNoticePage({Key? key}) : super(key: key);

  @override
  _SiftNoticePageState createState() => _SiftNoticePageState();
}

class _SiftNoticePageState extends State<SiftNoticePage> {
  List<Article> topArticles = [];
  ///状态
  late int status = LoadStatusType.loading;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getArticle();
  }

  //公告
  void getArticle() async{
    try{
      topArticles = await ServiceRepository.fetchBanners(type: 2,);
      setState(() {
        status = LoadStatusType.success;
      });
    }catch (e, s) {
      status = LoadStatusType.empty;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "公告",
        actionName: "全部已读",
      ),
      body: Builder(
        builder: (context) {
          if (status != LoadStatusType.success) {
            return StatusViews(status);
          }
          return ListView.builder(
            itemCount: topArticles.length,
            itemBuilder: (context, int index) {
              Article model = topArticles[index];
              return _buildItem(model);
            },
          );
        }
      ),

    );
  }

  Widget _buildItem(Article model) {
    return InkWell(
      onTap: () async {
        // await ServiceRepository.fetchBanners(type: 2,);
        Get.to(AnnouncementPage(title: model.title??"",content:model.content??"",));
      },
      child: Container(
        height: 65,
        padding: EdgeInsets.symmetric(horizontal: 16),
        decoration: BoxDecoration(
            border: Border(
              bottom: Divider.createBorderSide(context,
                  color: Global.kTheme.colorLine, width: 1),
            )),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 12,
                ),
                Text(
                  model.title??"",
                  style: TextStyle(
                      fontSize: 14,
                      fontFamily: Global.mySemiboldFont,
                      color: Global.kTheme.textPrimary6),
                ),
                SizedBox(
                  height: 2,
                ),
                Text(
                  model.createTime??"",
                  style: TextStyle(
                      fontSize: 12,
                      fontFamily: Global.myRegularFont,
                      color: Global.kTheme.inputHint1),
                ),
              ],
            ),
            Spacer(),
            // Container(
            //   width: 6,
            //   height: 6,
            //   decoration: BoxDecoration(
            //     color: Colors.red,
            //     borderRadius: BorderRadius.circular(4),
            //   ),
            // ),
            SizedBox(width: 5,),
            LoadAssetImage(
              "next_icon",
              width: 7,
              height: 12,
            ),
          ],
        ),
      ),
    );
  }
}

